<template>
  <div>
    <!-- 头部搜索区域 -->
    <el-form :inline="true" :model="formOne" class="demo-form-inline">
      <el-form-item>
        <el-select v-model="formOne.status" placeholder="状态" clearable>
          <el-option label="未审核" value="未审核" />
          <el-option label="审核通过" value="审核通过" />
          <el-option label="审核不通过" value="审核不通过" />
          <el-option label="推荐" value="推荐" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="formOne.title"
          placeholder="标题"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="formOne.categoryId"
          placeholder="请选择栏目"
          clearable
        >
          <el-option
            v-for="item in allCategory"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="toSearch(formOne, params)"
        >查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          style="margin-left: 520px"
          @click="toPublish"
        >发布风采</el-button>
      </el-form-item>
    </el-form>
    <!-- 头部搜索区域结束 -->

    <!-- 表格区域 -->
    <el-table
      :data="infoData"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="index" :index="1" label="序号" width="60" />
      <el-table-column prop="cover" label="封面" width="140">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img
              slot="reference"
              :src="scope.row.cover"
              width="80px"
              alt="加载失败"
            >
            <img width="200px" height="200px" :src="scope.row.cover" alt="">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="风采" width="840">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span><br>
          <span v-if="scope.row.baseUser == null">null</span>
          <span v-else-if="scope.row.baseUser">{{
            scope.row.baseUser.realname
          }}</span><br>
          <span>{{ scope.row.category.name }}</span><br>
          <span>{{ scope.row.publishTime | fmtDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === '审核通过'"
            type="success"
          >审核通过</el-tag>
          <el-tag
            v-else-if="scope.row.status === '未审核'"
            type="info"
          >未审核</el-tag>
          <el-tag
            v-else-if="scope.row.status === '推荐'"
            type="warning"
          >推荐</el-tag>
          <el-tag v-else type="danger">审核不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="scope">
          <a
            href="#"
            class="recommend"
            @click.prevent="recommend(scope.row.id, (status = '推荐'))"
          >推荐</a>
          <a
            href="#"
            class="auditing"
            @click.prevent="
              auditing(scope.row.title, scope.row.content, scope.row.id)
            "
          >审核</a>
          <a
            href="#"
            class="delete"
            @click.prevent="toDelete(scope.row.id)"
          >删除</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域结束 -->

    <!-- 分页组件区域 -->
    <el-pagination
      class="page"
      :current-page="params.page"
      :page-sizes="[2, 5, 10, 15, 20]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 分页组件区域结束 -->

    <!-- 审核模态框区域 -->
    <el-dialog title="审核风采信息" :visible.sync="visible" width="50%">
      <div style="text-align: center; font-size: 18px">{{ title }}</div>
      <div style="margin-top: 20px" v-html="content" />
      <span slot="footer" class="dialog-footer">
        <hr style="color: #30b08f">
        <el-button
          type="danger"
          @click="auditingTwo(id, (status = '审核不通过'))"
        >审核不通过</el-button>
        <el-button
          type="primary"
          @click="auditingThree(id, (status = '审核通过'))"
        >审核通过</el-button>
      </span>
    </el-dialog>
    <!-- 审核模态框区域结束 -->
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
import fmtDate from '@/filters/index'
export default {
  data() {
    return {
      // 模态框显示与隐藏
      visible: false,
      // 模态框标题与内容和id
      title: '',
      content: '',
      id: 0,
      // 表单数据
      formOne: {},
      // 栏目分类数据
      allCategory: [],
      // 分页数据
      params: {
        page: 1,
        pageSize: 10
      },
      // 资讯信息数据
      infoData: [],
      // 封面数据
      cover: '',
      // 分页总条数
      total: 0,
      // 风采状态
      status: ''
    }
  },
  created() {
    this.getAllCategory()
    this.getAllInfo()
  },
  methods: {
    // 发布风采按钮事件
    toPublish() {
      {
        this.$router.push({
          path: 'Publish'
        })
      }
    },

    // 删除按钮事件
    toDelete(id) {
      // console.log(id);
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await get('/article/deleteById', { id })
          // console.log(id);
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getAllInfo()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 审核按钮事件
    auditing(title, content, id) {
      this.visible = true
      this.title = title
      this.content = content
      this.id = id
    },

    // 审核不通过按钮事件
    async auditingTwo(id, status) {
      this.temp = { id, status }
      const res = await post('/article/changeStatus', this.temp)
      this.$message({
        type: 'success',
        message: '审核成功!'
      })
      this.getAllInfo()
      this.visible = false
    },

    // 审核通过按钮事件
    async auditingThree(id, status) {
      this.temp = { id, status }
      const res = await post('/article/changeStatus', this.temp)
      this.$message({
        type: 'success',
        message: '审核成功!'
      })
      this.getAllInfo()
      this.visible = false
    },

    // 推荐按钮事件
    recommend(id, status) {
      this.$confirm('您确定要推荐此资讯吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.status = status
          this.temp = { id, status }
          const res = await post('/article/changeStatus', this.temp)
          // console.log(res);
          this.$message({
            type: 'success',
            message: '审核成功!'
          })
          this.getAllInfo()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消推荐'
          })
        })
    },

    // 查询按钮事件
    async toSearch(formOne, params) {
      const temp = { ...formOne, ...params }
      const res = await get('/article/pageQuery', temp)
      this.infoData = res.data.list
      this.cover = res.data.list.cover
      this.total = res.data.total
      // console.log(res);
    },

    // 获取所有栏目分类
    async getAllCategory() {
      const res = await get('/category/findAll')
      this.allCategory = res.data
      // console.log(res.data);
    },

    // 分页查询所有资讯
    async getAllInfo() {
      const res = await get('/article/pageQuery', this.params)
      this.infoData = res.data.list
      this.cover = res.data.list.cover
      this.total = res.data.total
      console.log(res.data.list)
    },

    // 页码条数发生改变事件  默认参数pageSize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.params.page = 1
      this.getAllInfo()
    },
    // 当前页码发生触发的方法 默认参数是page
    handleCurrentChange(page) {
      this.params.page = page
      this.getAllInfo()
    }
  }
}
</script>
<style lang="scss">
.delete {
  color: #e65d6e;
}
.recommend {
  color: #30b08f;
  margin-right: 10px;
}
.auditing {
  color: #3a71a8;
  margin-right: 10px;
}
.page {
  float: right;
}
.el-dialog__header {
  background-color: #30b08f;
  .el-dialog__title {
    color: white;
  }
}
.el-dialog__headerbtn .el-dialog__close {
  color: white;
}
.el-button {
  cursor: pointer;
}
</style>
